package us.mooresr.webtroopmanager.client.activity;

import us.mooresr.webtroopmanager.client.ClientFactory;
import us.mooresr.webtroopmanager.client.SecurityManager;
import us.mooresr.webtroopmanager.client.TroopManagerService;
import us.mooresr.webtroopmanager.client.TroopManagerServiceAsync;
import us.mooresr.webtroopmanager.client.place.DashboardPlace;
import us.mooresr.webtroopmanager.client.place.LoginPlace;
import us.mooresr.webtroopmanager.client.ui.AdminView;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;


public class AdminActivity extends MyActivity implements AdminView.Presenter {

	
	public AdminActivity(ClientFactory clientFactory) {
		super(clientFactory);
	}


	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		super.start(panel, eventBus);
		
		AdminView adminView = clientFactory.getAdminView();
		adminView.setPresenter(this);
		panel.setWidget(adminView.asWidget());			
	}

	@Override
	public void doPreloadPeople() {
		if (Window.confirm("Delete all people and reload from static data?")) {
			TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT
					.create(TroopManagerService.class);
			AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onSuccess(Integer result) {
					Window.alert(result.toString() + " entries added.");

				}
			};
			troopManagerService.preloadData(callback);
		}
		
	}
	
	@Override
	public void doCreateUser(String user, String password, String realName, String emailAddress) {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Create User  failed");				
			}

			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					Window.alert("User created");
				} else {
					Window.alert("User not created");
				}
				
			}
		};
		
		troopManagerService.createLogin(user, password, realName, emailAddress, true, callback);
		
	}

	@Override
	public void previousPlace() {
		SecurityManager securityManager = clientFactory.getSecurityManager();
		if (securityManager.getUserId() == null) {
			goTo(new LoginPlace());
		} else {
			goTo(new DashboardPlace());
		}
		
		
	}

	@Override
	public void doInitializeUsers() {
		if (Window.confirm("Delete all users except the admin user?")) {
			TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT
					.create(TroopManagerService.class);
			AsyncCallback<Void> callback = new AsyncCallback<Void>() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onSuccess(Void result) {
					Window.alert("User database reset");

				}
			};
			troopManagerService.initializeUserDatabase(callback);
		}
		
	}
}
